
let points = [[20, 20], [device.width - 20, 20], [20, device.height - 20], [device.width - 20, device.height - 20]]
let saveColors = []
let saveTime = 0
let isStuck = false
let stuckTime = 0

/** 游戏卡住检测 */
module.exports.gameStuckDetection = function (duration) {
    if(!duration) duration = 5

    let img = images.captureScreen()
    let isSome = true

    points.forEach((p, idx) => {
        let c = colors.toString(images.pixel(img, p[0], p[1]))

        if (saveColors[idx] != c) {
            isSome = false
            saveColors[idx] = c
        }
    });

    if (isSome) {
        let time = (new Date().valueOf() - saveTime)
        stuckTime = (time / 1000 / 60).toFixed(1)
        isStuck = time > 1000 * 60 * duration
    } else {
        saveTime = new Date().valueOf()
        isStuck = false
        console.log('游戏恢复正常');
    }
    

    sleep(100 * 1)
    if (isStuck) {
        console.log('游戏已卡住 ' + stuckTime + ' 分钟');

        events.broadcast.emit("detection");
    }
}

